package man4j;

import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;

public class CommonTaskScreen extends AbstractEditTaskScreen {
	public static final String EDIT_COMMON_TASKS = "Common tasks";
	
	private Command deleteCommand = new Command("Delete", Command.ITEM, 1);
	
	private Command copyCommand = new Command("To current", Command.ITEM, 1);
	
	private Vector taskCommands = new Vector();
	
	private ItemCommandListener taskCommandListener;
	
	public CommonTaskScreen(final MainMIDlet mainMIDlet) {
		super(mainMIDlet, EDIT_COMMON_TASKS);
		
		taskCommands.addElement(copyCommand);
		taskCommands.addElement(deleteCommand);
		
		taskCommandListener = new ItemCommandListener() {
			public void commandAction(Command c, Item item) {
				Task t = (Task) item;
				
				if (c == deleteCommand) {
					delete(getItemNum(item));					
				}
				
				if (c == copyCommand) {
					Task copyOfTask = new Task(t.getString(), false);
					
					mainMIDlet.getCurrentTasksScreen().configureTask(copyOfTask);
					mainMIDlet.getCurrentTasksScreen().append(copyOfTask);
				}
			}
		};
	}
	
	protected void onSave() {
		Vector tasks = new Vector();

		for (int i = 0; i < size(); i++) {
			Task t = (Task) get(i);
			
			if (t.isDirty()) {
				tasks.addElement(get(i));
			}
		}
		
		TaskStore.saveTasks(getClass().getName(), tasks);		
	}
	
	protected Vector getTaskCommands() {	
		return taskCommands;
	}
	
	protected ItemCommandListener getTaskCommandListener() {	
		return taskCommandListener;
	}
}
